#include "app_mqtt.h"
// 服务器地址
#define MQTT_SERVERURI "tcp://192.168.55.72:1883"
// 客户端id
#define MQTT_CLIENTID "linux gate"
// 订阅主题
#define MQTT_SUBSCRIBE_TOPIC "pull"
// 发布主题
#define MQTT_PUBLISH_TOPIC "push"

// 连接丢失回调
void Mqtt_ConLost(void* context, char* cause);
// 接收到消息回调
int Mqtt_ReceiveMessage(void *context, char *topicName, int topicLen, MQTTClient_message *message);
// 消息发送完成回调
void Mqtt_DeliveryComplete(void *context, MQTTClient_deliveryToken dt);

typedef struct
{
    MQTTClient mqttclient;
    void (*receivecallback)(char *data, int len);
} app_mqtt;
static app_mqtt app_mqtt_t;

GateState_t App_Mqtt_Init(void (*receivecallback)(char *data, int len))
{
    // 回调函数赋值
    app_mqtt_t.receivecallback = receivecallback;
    // 创建客户端
    if (MQTTClient_create(&app_mqtt_t.mqttclient, MQTT_SERVERURI, MQTT_CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL) != MQTTCLIENT_SUCCESS)
    {
        log_error("创建mqtt客户端失败。。。");
        return GATE_ERROR;
    }
    log_info("创建mqtt客户端成功。。。");
    // 设置回调函数
    if (MQTTClient_setCallbacks(app_mqtt_t.mqttclient, NULL, Mqtt_ConLost, Mqtt_ReceiveMessage, Mqtt_DeliveryComplete) != MQTTCLIENT_SUCCESS)
    {
        log_error("设置回调函数失败。。。");
        return GATE_ERROR;
    }
    log_info("设置回调函数成功。。。");

    // 连接服务器
    MQTTClient_connectOptions config = MQTTClient_connectOptions_initializer;
    if (MQTTClient_connect(app_mqtt_t.mqttclient, &config) != MQTTCLIENT_SUCCESS)
    {

        log_error("mqtt服务器连接失败。。。");
        return GATE_ERROR;
    }
    log_info("mqtt服务器连接成功。。。");

    // 订阅主题
    if (MQTTClient_subscribe(app_mqtt_t.mqttclient, MQTT_SUBSCRIBE_TOPIC, 0) != MQTTCLIENT_SUCCESS)
    {
        log_error("mqtt订阅主题失败。。。");
        return GATE_ERROR;
    }
    log_info("mqtt订阅主题成功。。。");
    return GATE_OK;
}

// 连接丢失回调
void Mqtt_ConLost(void* context, char* cause)
{
    // 重新连接服务器
    while (1)
    {
        MQTTClient_connectOptions config = MQTTClient_connectOptions_initializer;
        if (MQTTClient_connect(app_mqtt_t.mqttclient, &config) != MQTTCLIENT_SUCCESS)
        {

            log_error("mqtt服务器重新连接失败。。。");
            sleep(2);
            continue;
        }
        log_info("mqtt服务器重新连接成功。。。。");
        break;
    }
    while (1)
    {
        if (MQTTClient_subscribe(app_mqtt_t.mqttclient, MQTT_SUBSCRIBE_TOPIC, 0) != MQTTCLIENT_SUCCESS)
        {
            log_error("mqtt重新订阅主题失败。。。");
            sleep(2);
            continue;
        }
        log_info("mqtt订阅主题成功。。。");
        break;
    }
}
// 接收到消息回调
int Mqtt_ReceiveMessage(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    if (app_mqtt_t.receivecallback)
    {
        app_mqtt_t.receivecallback(message->payload, message->payloadlen);
    }
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}
// 消息发送完成回调
void Mqtt_DeliveryComplete(void *context, MQTTClient_deliveryToken dt)
{
    log_info("消息发送完成。。。。。");
}
// 向主题中发布消息
GateState_t App_Mqtt_PublishMessage(char *message, int len)
{
    if (MQTTClient_publish(app_mqtt_t.mqttclient, MQTT_PUBLISH_TOPIC, len, (void *)message, 0, 0, NULL) != MQTTCLIENT_SUCCESS)
    {
        log_error("mqtt消息发布失败。。。");
        return GATE_ERROR;
    }
    log_info("mqtt消息发布成功。。。。");
    return GATE_OK;
}

void App_Mqtt_Deinit(void){
   //取消订阅主题
   MQTTClient_unsubscribe(app_mqtt_t.mqttclient,MQTT_SUBSCRIBE_TOPIC);
   //断开连接
   MQTTClient_disconnect(app_mqtt_t.mqttclient,1000);
   //注销客户端
   MQTTClient_destroy(&app_mqtt_t.mqttclient);

}